package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.bean.BusinessAbodeBean;
import com.koron.css2.baseConfig.bean.PrefStrategyBean;
import com.koron.css2.baseConfig.bean.TradeClassifyBean;
import com.koron.css2.baseConfig.mapper.*;
import com.koron.css2.clientCenter.bean.*;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.MeterService;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.serviceManage.bean.DistrictBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DistrictMapper;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 变更详情信息
 * 
 * @author 甄玉晴
 *
 */
public class ChangeUserQuery implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			MessageBean<UserChangeDetailBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功",
					UserChangeDetailBean.class);
			UserChangeBean bean = JsonUtils.objectToPojo(req.getData(), UserChangeBean.class);
			// 传参不能为空
			if (bean == null || StringUtils.isEmpty(bean.getId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "查询失败,缺少主键", null);
			}
			// 获取变更记录信息
			ChangeUserMapper mapper = factory.getMapper(ChangeUserMapper.class);
			bean = mapper.getUserChangeInfoById(bean.getId());
			// 获取修改后账户、客户、用户信息
			AccountInfoMapper accountMapper = factory.getMapper(AccountInfoMapper.class);
			CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
			UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
			PubAdminAreaMapper pmapper = factory.getMapper(PubAdminAreaMapper.class,"_default");
			// 获取用户信息
			UserBean user = userMapper.getUserInfoById(bean.getAfterUserInfoTempId(), UserBean.USERTEMP);
			user.setClientContractAddrId(user.getClientContractAddr());
			
			if(!StringUtils.isEmpty(user.getClientContractAddr())) {
				String area = pmapper.selectFullNameById(user.getClientContractAddr());
				user.setClientContractAddr(area);
			}
			// 获取营业所名称
			if (!StringUtils.isEmpty(user.getBusinessAbode())) {
				BusinessAbodeMapper busniessAbodemapper = factory.getMapper(BusinessAbodeMapper.class);
				BusinessAbodeBean abode = busniessAbodemapper.findBusinessAbodeById(user.getBusinessAbode());
				user.setBusinessAbodeName(abode==null?"":abode.getAbodeName());
			}
			// 获取营业区域
			if (!StringUtils.isEmpty(user.getBusinessArea())) {
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				user.setBusinessAreaName(
						businessAreaMapper.findBusinessAreaById(user.getBusinessArea(), userInfo.getCurWaterCode()));
			}
			// 获取行政区域
			if (!StringUtils.isEmpty(user.getAdminArea())) {
				DistrictMapper districtMapper = factory.getMapper(DistrictMapper.class);
				DistrictBean district = districtMapper.findDistrictById(user.getAdminArea());
				user.setAdminAreaName(district==null?"":district.getSname());
			}
			// 获取优惠策略名称
			if (!StringUtils.isEmpty(user.getPrefStrategyId())) {
				PrefStrategyMapper prefStrategyMapper = factory.getMapper(PrefStrategyMapper.class);
				PrefStrategyBean prefStrategy = prefStrategyMapper.selectPrefStrategyById(user.getPrefStrategyId());
				user.setPrefStrategyName(prefStrategy==null?"":prefStrategy.getStrategyName());
			}
//			// 获取用水类型
//			if (!StringUtils.isBlank(user.getUseWaterType())) {
//				WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
//				user.setUseWaterTypeName(waterTypeMapper.findWaterTypeById(user.getUseWaterType()).getWaterTypeName());
//			}
			// 获取账户信息
			AccountInfoBean accountInfo = accountMapper.getAccountInfoById(bean.getAfterAccountInfoTempId(),
					AccountInfoBean.ACCOUNTTEMP);
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getAccountType())) {
				accountInfo.setAccountTypeName(Tools.getDicNameByCodeAndValue(factory, "ACT", accountInfo.getAccountType()));
			}
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getPayWay())) {
				accountInfo.setPayWayName(Tools.getDicNameByCodeAndValue(factory, "BPW", accountInfo.getPayWay()));
			}
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getInvoiceType())) {
				accountInfo.setInvoiceTypeName(Tools.getDicNameByCodeAndValue(factory, "BBT", accountInfo.getInvoiceType()));
			}
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getDeliverWay())) {
				accountInfo.setDeliverWayName(Tools.getDicNameByCodeAndValue(factory, "SDW", accountInfo.getDeliverWay()));
			}
			if(StringUtils.isNotBlank(accountInfo.getVatFlag()) && "1".equals(accountInfo.getVatFlag())) {
				accountInfo.setVatFlagName("是");
			}else {
				accountInfo.setVatFlagName("否");
			}
			BankMapper bankeMapper = factory.getMapper(BankMapper.class);
			// 获取开户行名称
			if (!StringUtils.isBlank(accountInfo.getOpenAccountBank())) {
				BankBean banke = bankeMapper.findBankById(accountInfo.getOpenAccountBank());
				accountInfo.setOpenAccountBankName(banke==null?"":banke.getBankName());
			}
			// 获取购买方开户行名称
			if (!StringUtils.isBlank(accountInfo.getInvoiceBank())) {
				BankBean banke = bankeMapper.findBankById(accountInfo.getInvoiceBank());
				accountInfo.setInvoiceBankName(banke==null?"":banke.getBankName());
			}
			//user字段转换
			Map<String, String> cus = Tools.mapDicByCode(factory, "CUS");//用户状态
			Map<String, String> mrm = Tools.mapDicByCode(factory, "MRM");//拆表类型
			Map<String, String> MMT = Tools.mapDicByCode(factory, "MMT");//水表类型
			Map<String, String> uws = Tools.mapDicByCode(factory, "UWS");//用水对象
			Map<String, String> cmc = Tools.mapDicByCode(factory, "CMC");//水表用途
			Map<String, String> csw = Tools.mapDicByCode(factory, "CSW");//分摊方式
			Map<String, String> ctl = Tools.mapDicByCode(factory, "CTL");//装表位置
			Map<String, String> rtp = Tools.mapDicByCode(factory, "RTP");//抄表周期
			//客户信息
			Map<String, String> cdl = Tools.mapDicByCode(factory, "CDL");//信用等级
			Map<String, String> ctt = Tools.mapDicByCode(factory, "CTT");//客户类型
			Map<String, String> idt = Tools.mapDicByCode(factory, "IDT");//证件类型
			
			// 获取行业分类名称
			if (!StringUtils.isEmpty(user.getTradeClassify())) {
				TradeClassifyMapper tradeMapper = factory.getMapper(TradeClassifyMapper.class);
				TradeClassifyBean tbean = tradeMapper.selectTradeClassifyById(user.getTradeClassify());
				if(tbean!=null) {
					user.setTradeClassify(tbean.getTradeName());
				}
			}
			if(user!=null) {
				user.setStatusName(cus.get(user.getStatus()));
				user.setSplitMeterWayName(mrm.get(user.getSplitMeterWay()));
				user.setMeterTypeName(MMT.get(user.getMeterType()));
				user.setUseWaterObjectName(uws.get(user.getUseWaterObject()));
				user.setMeterUseName(cmc.get(user.getMeterUse()));
				user.setShareWayName(csw.get(String.valueOf(user.getShareWay())));
				user.setSetupMeterLocationName(ctl.get(user.getSetupMeterLocation()));
				user.setMeterReadingCycleName(rtp.get(user.getMeterReadingCycle()));
				//表井位置
				if (StringUtils.isNotBlank(user.getMeterBjwz())) {
					user.setMeterBjwzName(Tools.getDicNameByCodeAndValue(factory, "BJWZ", user.getMeterBjwz()));
				}
				//表井形式
				if (StringUtils.isNotBlank(user.getMeterBjxs())) {
					user.setMeterBjxsName(Tools.getDicNameByCodeAndValue(factory, "BJXS", user.getMeterBjxs()));
				}
			}
			
			// 获取客户信息
			CtmInfoBean ctmInfo = ctmMapper.getCtmInfoById(bean.getAfterCtmInfoTempId(), CtmInfoBean.CTMTEMP);
			
			if(!StringUtils.isEmpty(ctmInfo.getClientAddr())) {
				String area = pmapper.selectFullNameById(ctmInfo.getClientAddr());
				ctmInfo.setClientAddr(area);
			}
			// 转换状态字段
			if(ctmInfo!=null) {
				ctmInfo.setCreditLevelName(cdl.get(ctmInfo.getCreditLevel()));
				ctmInfo.setCtmTypeName(ctt.get(ctmInfo.getCtmType()));
				ctmInfo.setCertTypeName(idt.get(ctmInfo.getCertType()));
			}
			
			
			// 获取用户人口信息
			List<UserChangePersonBean> person = mapper.getUserChangePersonById(bean.getId());
			// 获取水表信息
			MsInfoDto msInfo = null;
			if (!StringUtils.isEmpty(user.getMeterNo())) {
				MeterService meterSrvice = new MeterService();
				msInfo = ADOConnection.runTask("_default", meterSrvice, "getMsInfoByNo", MsInfoDto.class,
						user.getMeterNo(),userInfo.getCurWaterCode());
			}
			// 返回详情信息
			UserChangeDetailBean detail = new UserChangeDetailBean();
			detail.setAccountInfo(accountInfo);
			detail.setCtmInfo(ctmInfo);
			detail.setUserChangeInfo(bean);
			detail.setUserInfo(user);
			detail.setPerson(person);
			detail.setMsInfo(msInfo);
			info.setData(detail);
			return info;
		} catch (Exception e) {
			logger.error("变更信息查询失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
